//
//  WSJTexfildForSuggestionList.m
//  自动补全的textfield
//
//  Created by wsj2015 on 16/5/24.
//  Copyright © 2016年 wsj2015. All rights reserved.
//

#import "WSJTexfildForSuggestionList.h"
#import "UITableViewCell+UITableViewCell_selectAnimation.h"

@interface WSJTexfildForSuggestionList ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,  strong) UITableView *tableView;

@property (nonatomic,  strong) NSMutableArray *dataArray;

@property (nonatomic,  assign) CGFloat customHeight;

@end

@implementation WSJTexfildForSuggestionList

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellId"];
//        [self.superview addSubview:_tableView];
        
    }
    return _tableView;
}
- (void)setCustomFrame:(CGRect )customFrame
{
    self.tableView.frame = customFrame;
    self.customHeight = customFrame.size.height;
}
- (void)setAbleSuggestinglist:(BOOL)ableSuggestinglist{
    if (ableSuggestinglist == NO) {
        return;
    }
    CGRect selfFrame =self.frame;
    self.tableView.frame =CGRectMake(selfFrame.origin.x, CGRectGetMaxY(selfFrame), selfFrame.size.width, 0);
    self.tableView.layer.borderColor = kBorderColor.CGColor;
    self.tableView.layer.borderWidth = kOnePX;
    
    self.tableView.layer.cornerRadius = 5;
    self.tableView.layer.masksToBounds = YES;
    [self showTableView];
    
}
- (void)setSuperView:(UIView *)superView
{
    [superView addSubview:self.tableView];
}
- (void)setSuggestionArray:(NSArray *)suggestionArray
{
    if (suggestionArray &&suggestionArray.count>0) {
        self.dataArray  = [suggestionArray copy];
        [self.tableView reloadData];
    }

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cellId" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    UIView *backView =[UIView new];
    backView.backgroundColor = [UIColor orangeColor];
    cell.selectedBackgroundView =backView;
    NSDictionary *dict =self.dataArray[indexPath.row];
    if ([dict isKindOfClass:[NSDictionary class]]) {
        cell.textLabel.text = [NSString stringWithFormat:@"%@ (%@)",dict[@"phone"],dict[@"name"]];
    }
    else{
        NSString *account =self.dataArray[indexPath.row];
        cell.textLabel.text = account;
    }
    
    cell.textLabel.font =[UIFont systemFontOfSize:14];

    cell.textLabel.textColor = kTxtColor;
    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 33;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cellId" forIndexPath:indexPath];
//    [cell selectAnimationwithWidth:320 height:33];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.callBack) {
        NSDictionary *dict =self.dataArray[indexPath.row];
        if ([dict isKindOfClass:[NSDictionary class]]) {
            
            self.callBack(dict[@"phone"]);
        }
        else{
            NSString *account =self.dataArray[indexPath.row];
             self.callBack(account);
        }

       
    }
    CGRect frame=  self.tableView.frame;
    frame.size.height =0;
    [UIView animateWithDuration:.35f animations:^{
        self.tableView.frame = frame;
    } completion:^(BOOL finished) {
        [self.tableView removeFromSuperview];
        
    }];
    
   
    
     
}
- (void)hideList{
    CGRect frame=  self.tableView.frame;
    
    frame.size.height =0;
    [UIView animateWithDuration:.35f animations:^{
        self.tableView.frame = frame;
    } completion:^(BOOL finished) {
        [self.tableView removeFromSuperview];
        
    }];
}
- (void)showTableView{
    CGRect frame=  self.tableView.frame;
    frame.size.height =self.customHeight;
    [UIView animateWithDuration:.35f animations:^{
        self.tableView.frame = frame;
    } completion:^(BOOL finished) {
       
        
    }];
}
@end
